﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarMind.Dialogs
{
    /// <summary>
    /// TextInputBox.xaml 的交互逻辑
    /// </summary>
    public partial class PaddingInputBox : Window
    {
        public PaddingInputBox(Window owner, string promptText, string title, string initilizedText)
        {
            InitializeComponent();

            this.Owner = owner;

            LbPrompt.Text = promptText;
            this.Title = (title == null ? string.Empty : title);
            this.TbxInput.Text = (initilizedText == null ? string.Empty : initilizedText);
        }

        private Thickness? resultThickness;

        public Thickness? ResultThickness
        {
            get { return resultThickness; }
        }

        private ThicknessConverter tc = new ThicknessConverter();

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.resultThickness = null;//无值。
            this.DialogResult = false;
            this.Close();
        }

        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            Thickness newThickness;
            try
            {
                string text = TbxInput.Text;

                text = text.Replace("１", "1");
                text = text.Replace("２", "2");
                text = text.Replace("３", "3");
                text = text.Replace("４", "4");
                text = text.Replace("５", "5");
                text = text.Replace("６", "6");
                text = text.Replace("７", "7");
                text = text.Replace("８", "8");
                text = text.Replace("９", "9");
                text = text.Replace("０", "0");
                text = text.Replace("，", ",");
                text =
                text = text.Replace("|", ",");
                text = text.Replace("｜", ",");
                text = text.Replace("｜｜", ",");
                text = text.Replace("||", ",");
                text =
                text = text.Replace("。", ",");

                newThickness = (Thickness)tc.ConvertFrom(text);

                if (newThickness.Left < 0) newThickness.Left = 0;
                if (newThickness.Right < 0) newThickness.Right = 0;
                if (newThickness.Top < 0) newThickness.Top = 0;
                if (newThickness.Bottom < 0) newThickness.Bottom = 0;

                resultThickness = newThickness;
                this.DialogResult = true;
                this.Close();
            }
            catch (FormatException ex)
            {
                resultThickness = null;
                TbErrorMessage.Text = ex.Message;
                return;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.TbxInput.SelectAll();
            this.TbxInput.Focus();
        }
    }
}
